#include <iostream>
using std::cout;
using std::endl;



class FunctionObject{
public:
    //第一个()与operator连在一起作为函数名
    //第二个()代表参数列表
    void operator()(){
        ++_count;
        cout << "hello" << endl;
    }

    int operator()(int x,int y){
        ++_count;
        return x + y;
    }

    double operator()(double x){
        ++_count;
        return x;
    }

    int getCount() const{
        return _count;
    }
private:
    int _count = 0;
};

int count = 0;

void print(){
    ++count;
    cout << "hello" << endl;
}

int print(int x,int y){
    ++count;
    return x + y;
}

double print(double x){
    ++count;
    return x;
}

void test0(){
    FunctionObject fo;
    //函数调用运算符很特别
    //函数的返回值、参数是根据代码设计灵活设置的
    //
    //fo.operator()();
    fo();  //让对象像一个函数一样被调用
    cout << fo(1,2) << endl;
    cout << fo(3.4) << endl;

    //函数对象可以携带状态
    /* fo._count = 0; */
    /* cout << "count:" << fo._count << endl; */
    cout << "count:" << fo.getCount() << endl;

    
    cout << endl;
    print(); 
    cout << print(1,2) << endl;
    cout << print(3.4) << endl;

    //普通函数如果想要记录调用次数
    //只能通过全局变量
    //全局变量不安全
    count = 0;
    cout << "count:" << count << endl;
}


int main(void){
    test0();
    return 0;
}
